package com.yu.xcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;
import com.yu.xcx.dto.Result;
import com.yu.xcx.dto.Shop2;
import com.yu.xcx.entity.Shop;
import com.yu.xcx.mapper.ShopMapper;
import com.yu.xcx.service.IShopService;
import com.yu.xcx.util.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import static com.yu.xcx.util.RedisConstants.XCX_SHOP_KEY;

@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper,Shop> implements IShopService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ShopMapper shopMapper;

    //添加商品
    @Override
    public Boolean saveShop(Shop shop) {
        return save(shop);
    }

    //根据商品id查询商品
    @Override
    public Result queryById(Integer id) {
        Shop shop = shopMapper.getById(id);
        String images = shop.getImages();
        List<String> imagesList = getImages(images);
        Shop2 shop2 = new Shop2();
        shop2.setShop(shop);
        shop2.setImagesList(imagesList);
        if(shop2 != null) {
            return Result.ok(shop2);
        }
        else return Result.ok("没有此商品！");
    }

    //根据商品id修改商品
    @Override
    //添加事务
    @Transactional
    public Result updateShop(Shop shop) {
        Integer id = shop.getId();
        // 1.先判断id是否为空
        if (id == null){
            return Result.fail("商品id不能为空");
        }
        // 2.先更新数据库
        //使用Mybatis-plus的方法
        updateById(shop);
        // 3.再删缓存
        stringRedisTemplate.delete(XCX_SHOP_KEY + id);

        return Result.ok();
    }

    @Override
    public Result getAllShop() {
        int num = count();
        System.out.println("商品总数为===========" + num );
        int pageNum = num / 8;
        Random r = new Random();
        Page<Shop> page = query().
                page(new Page<>(r.nextInt(pageNum), 8));
        List<Shop> shopList = page.getRecords();
        List<Shop2> shop2List = new ArrayList<>();
        for (Shop s : shopList){
            Shop2 shop2 = new Shop2();
            String image = s.getImages();
            shop2.setShop(s);
            shop2.setImagesList(getImages(image));
            shop2List.add(shop2);
        }
        // 返回数据
        if (shop2List != null) return Result.ok(shop2List, (long) r.nextInt(pageNum));
        else return Result.ok("搜索列表为空！");
    }

    @Override
    public Result queryByName(String name, Integer pagenum) {
        Page<Shop> page = query()
                .like("name", name)
                .page(new Page<>(pagenum, SystemConstants.DEFAULT_PAGE_SIZE));
        List<Shop> shopList = page.getRecords();
        List<Shop2> shop2List = new ArrayList<>();
        for (Shop s : shopList){
            Shop2 shop2 = new Shop2();
            String image = s.getImages();
            shop2.setShop(s);
            shop2.setImagesList(getImages(image));
            shop2List.add(shop2);
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name", name);

        Long total = (long) count(queryWrapper);
        // 返回数据
        if (shop2List != null) return Result.ok(shop2List, total);
        else return Result.ok("搜索列表为空！");
    }

    @Override
    public Result queryByType(Integer typeId, Integer pagenum) {
        Page<Shop> page = query()
                .eq("type_id", typeId)
                .page(new Page<>(pagenum, SystemConstants.DEFAULT_PAGE_SIZE));
        List<Shop> shopList = page.getRecords();
        List<Shop2> shop2List = new ArrayList<>();
        for (Shop s : shopList){
            Shop2 shop2 = new Shop2();
            String image = s.getImages();
            shop2.setShop(s);
            shop2.setImagesList(getImages(image));
            shop2List.add(shop2);
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type_id", typeId);

        Long total = (long) count(queryWrapper);
        // 返回数据
        if (shop2List != null) return Result.ok(shop2List, total);
        else return Result.ok("搜索列表为空！");
    }

    @Override
    public List<String> getImages(String images) {
        String[] imagesArray = images.split(",");
        List<String> imagesList = new ArrayList<>();
        Collections.addAll(imagesList, imagesArray);
        return imagesList;
    }

    @Override
    public Result deleteById(Integer shopId) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", shopId);
        Boolean flag = remove(queryWrapper);
        return Result.ok(flag);
    }

    @Override
    public Result queryByUser(Integer userId, Integer pagenum) {
        Page<Shop> page = query()
                .eq("user_id", userId)
                .page(new Page<>(pagenum, SystemConstants.DEFAULT_PAGE_SIZE));
        List<Shop> shopList = page.getRecords();
        List<Shop2> shop2List = new ArrayList<>();
        for (Shop s : shopList){
            Shop2 shop2 = new Shop2();
            String image = s.getImages();
            shop2.setShop(s);
            shop2.setImagesList(getImages(image));
            shop2List.add(shop2);
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);

        Long total = (long) count(queryWrapper);
        // 返回数据
        if (shop2List != null) return Result.ok(shop2List, total);
        else return Result.ok("搜索列表为空！");
    }

    @Override
    public Result getShopsById(List<Integer> shopsList, Integer pagenum) {
        Page<Shop> page = query().in("shop_id",shopsList).page(new Page<>(pagenum, SystemConstants.DEFAULT_PAGE_SIZE));
        return Result.ok(page.getRecords());
    }

}
